import { checkAccount } from 'mock/tools';
import { userInfo } from '../../configs';
import { accountList } from '../Account/account';

/** 用户相关 */
const user = [
  /** 获取用户信息 */
  {
    url: '/api/getUserInfo',
    method: 'post',
    response: () => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };

      return { code: 200, data: userInfo };
    }
  },

  /** 编辑用户信息 */
  {
    url: '/api/editUserInfo',
    method: 'post',
    response: (options: { body: Record<string, any> }) => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };

      const { body } = options;
      for (const key in body) {
        if (userInfo[key]) {
          userInfo[key] = body[key];
        }
      }
      return { code: 200, data: userInfo };
    }
  },

  /** 获取用户列表 */
  {
    url: '/api/getUserList',
    method: 'post',
    response: () => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };

      const userList = accountList.map(p => {
        p.value = p.account;
        p.label = p.nickName;
        return p;
      })
      return { code: 200, data: userList };
    }
  }
];

export default user;
